<?php


namespace App\service\spider;

/**
 * @script   Myproducer.php
 * @brief    生产器Handler
 */
use Workerman\Timer;

class Myproducer extends \Webman\PHPCreeper\Producer
{
    /**
     * @brief   抓取未来7天内的天气预报DEMO
     *
     * @return  mixed
     */
    public function makeTask()
    {
        //Create One Task
        $task = array(
            'url' => 'http://www.weather.com.cn/weather/101010100.shtml',
            'rule' => array(
                'time' => ['div#7d ul.t.clearfix h1',      'text'],
                'wea'  => ['div#7d ul.t.clearfix p.wea',   'text'],
                'tem'  => ['div#7d ul.t.clearfix p.tem',   'text'],
                'wind' => ['div#7d ul.t.clearfix p.win i', 'text'],
            ),
            'context' => array(
                'cache_enabled'     => true,
                'cache_directory'   => '/tmp/DownloadCache4PHPCreeper/download/',
                'allow_url_repeat'  => true,
            ),
        );

        $this->newTaskMan()->createTask($task);
    }

    /**
     * @brief    onProducerStart
     *
     * @param    object $producer
     *
     * @return   mixed
     */
    public function onProducerStart($producer)
    {
        //$this->makeTask();
        Timer::add(2, [$this, "makeTask"], [], true);
    }

    /**
     * @brief    onProducerStop
     *
     * @param    object $producer
     *
     * @return   mixed
     */
    public function onProducerStop($producer)
    {
    }

    /**
     * @brief    onProducerReload
     *
     * @param    object $producer
     *
     * @return   mixed
     */
    public function onProducerReload($producer)
    {
    }

}